<?php

namespace app\supplier\model\coupon;

use app\common\model\plus\coupon\Coupon as CouponModel;
use app\common\model\plus\coupon\UserCoupon;
use app\common\model\region\Region;
use app\supplier\model\supplier\Supplier;

/**
 * 优惠券模型
 */
class Coupon extends CouponModel
{
    /**
     * 获取优惠券列表
     */
    public function getList($data, $shop_supplier_id)
    {
        $user_coupon = UserCoupon::getIsUerNumData();
        $list = $this->where('is_delete', '=', 0)
            ->where('shop_supplier_id', '=', $shop_supplier_id)
            ->order(['is_audit'=> 'asc','sort' => 'asc', 'create_time' => 'desc'])
            ->paginate($data);
        foreach ($list as $key => $value){
            $list[$key]['use_num'] = $user_coupon[$value['coupon_id']]['use_num']??0;
            $list[$key]['receive_num'] = $user_coupon[$value['coupon_id']]['receive_num']??0;

            // 20240615 Mr.Z 增加范围
            $sp_range = [];
            if($value['supplier_range'] == 0){
                $list[$key]['supplier_range_text'] = '全平台可用';
            }elseif($value['supplier_range'] == 1){
                $list[$key]['supplier_range_text'] = Region::getCityName($value['city_id']).'多个站点可用';
            }else{
                $list[$key]['supplier_range_text'] = '指定门店可用';
            }
            if($value['apply_range'] == 10){
                $list[$key]['apply_range_text'] = '正价商品可用';
            }else{
                $list[$key]['apply_range_text'] = '仅限指定商品可用';
            }
            isset($list[$key]['supplier_range_text']) && array_push($sp_range, $list[$key]['supplier_range_text']);
            isset($list[$key]['apply_range_text']) && array_push($sp_range, $list[$key]['apply_range_text']);
            $list[$key]['range'] = $sp_range;
            $list[$key]['res_nums'] = 1;
        }

        return $list;
    }

    /**
     * 添加新记录
     */
    public function add($data)
    {
        $data['app_id'] = self::$app_id;
        if ($data['expire_type'] == '20') {
            $data['start_time'] = strtotime($data['active_time'][0]);
            $data['end_time'] = strtotime($data['active_time'][1]);
        }
        $supplier = Supplier::detail($data['shop_supplier_id']);
        $data["province_id"] = $supplier["province_id"] ?: 0;
        $data["city_id"] = $supplier["city_id"] ?: 0;
        $data["county_id"] = $supplier["county_id"] ?: 0;
        $data["supplier_range"] = 2;
        $this->buildData($data);
        return self::create($data);
    }

    /**
     * 更新记录
     */
    public function edit($data)
    {
        if ($data['expire_type'] == '20') {
            $data['start_time'] = strtotime($data['active_time'][0]);
            $data['end_time'] = strtotime($data['active_time'][1]);
        }
        $supplier = Supplier::detail($data['shop_supplier_id']);
        $data["province_id"] = $supplier["province_id"] ?: 0;
        $data["city_id"] = $supplier["city_id"] ?: 0;
        $data["county_id"] = $supplier["county_id"] ?: 0;
        $data["supplier_range"] = 2;
        $this->buildData($data);
        return $this->save($data);
    }


    /**
     * 构造数据
     */
    private function buildData(&$data)
    {
        // 限制商品id
        if ($data['apply_range'] == 20) {
            $data['product_ids'] = implode(',', $data['product_ids']);
        }
        $category_first_ids = [];
        $category_second_ids = [];
        if ($data['apply_range'] == 30) {
            if (isset($data['category_list']['first'])) {
                foreach ($data['category_list']['first'] as $item) {
                    array_push($category_first_ids, $item['category_id']);
                }
            }
            if (isset($data['category_list']['second'])) {
                foreach ($data['category_list']['second'] as $item) {
                    array_push($category_second_ids, $item['category_id']);
                }
            }
            $data['category_ids'] = [
                'first' => $category_first_ids,
                'second' => $category_second_ids
            ];
            $data['category_ids'] = json_encode($data['category_ids']);
        }
    }

    /**
     * 删除记录 (软删除)
     */
    public function setDelete($where)
    {
        return self::update(['is_delete' => 1], $where);
    }

    /**
     * 查询指定优惠券
     * @param $value
     */
    public function getCoupon($value)
    {
        return $this->where('coupon_id', 'in', $value)->select();
    }

    /**
     * 查询指定优惠券
     * @param $value
     */
    public function getCoupons($value)
    {
        $data = $this->where('coupon_id', 'in', $value)->select();
        $name = '';
        if (!empty($data)) {
            foreach ($data as $val) {
                $name .= $val['name'] . ',';
            }
        }

        return $name;
    }
}
